package com.gmd.service;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import com.gmd.model.Cita;
import com.gmd.model.repository.CitaMedicaRepository;
import com.gmd.util.Utilitario;

public class CitaMedicaService {
	private CitaMedicaRepository citaRepository;
	private EntityManager em;
	
	public void CitaMedica(){
		em= Utilitario.getInstance().getEntityManager();
		citaRepository = new CitaMedicaRepository(em);
	}

	public void grabarCita(Cita a) throws RuntimeException{
		EntityTransaction tx=em.getTransaction();
		try{
			tx.begin();
		CitaMedicaRepository.insert(a);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
			throw new  RuntimeException("No se pudo generar cita");
		}
	}
	
	
	public List<Cita> listarCita(){
		return CitaMedicaRepository.listAll();
	}

	public void eliminarCita(Cita a){
		EntityTransaction tx=em.getTransaction();
		try{
			tx.begin();
			CitaMedicaRepository.delete(a);
			tx.commit();
		}catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
			throw new RuntimeException("No se pudo eliminar");
		}
	}

}